package com.steakliu.sun.disuse;

import com.steakliu.sun.disuse.api.ConnectionsInfo;
import com.steakliu.sun.disuse.api.DisuseStrategy;
import com.steakliu.sun.spi.annotation.Join;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能说明： 淘汰使用次数最少的连接
 * <p>
 * Original @Author: steakliu-刘牌, 2023-02-06  23:30
 * <p>
 * Copyright (C)2020-2022  steakliu All rights reserved.
 */
@Join
public class LFUDisuseStrategy implements DisuseStrategy {
    @Override
    public ConnectionsInfo selectConnection(List<ConnectionsInfo> connectionsInfoList) {
        return connectionsInfoList.stream().sorted(new Comparator<ConnectionsInfo>() {
            @Override
            public int compare(ConnectionsInfo o1, ConnectionsInfo o2) {
                return o1.getUseCount() - o2.getUseCount() > 0 ? 1 : -1;
            }
        }).collect(Collectors.toList()).get(0);
    }
}
